<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$types=array(1,2,3);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1);
	if($_RQ['level_id']){
		$params['level_id']=intval($_RQ['level_id']);
		$return['params']['level_id']=$_RQ['level_id'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(!empty($_RQ['y']) && !empty($_RQ['m'])){
		$yuestart = mktime(0,0,0,$_RQ['m'],1,$_RQ['y']);
		$yueend = mktime(0,0,0,$_RQ['m']+1,1,$_RQ['y']);
		$params['createtime between']=array($yuestart,$yueend);
	}elseif(!empty($_RQ['y'])){
		$yuestart = mktime(0,0,0,1,1,$_RQ['y']);
		$yueend = mktime(0,0,0,1,1,$_RQ['y']+1);
		$params['createtime between']=array($yuestart,$yueend);
	}
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc'
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=$item['province'].' '.$item['city'];
			$item['getfee0']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'status'=>1,'level'=>$types),'money');
			$item['getfee']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'level'=>$types),'money');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'address','title'=>'代理地区','type'=>'string'),
			array('name'=>'level_id','title'=>'等级','type'=>'radio','options'=>'level_types'),
			array('name'=>'getfee0','title'=>'待提现绩效','type'=>'string'),
			array('name'=>'getfee','title'=>'总绩效额度','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>10,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>10,'title'=>'月份','type'=>'radio','options'=>'months'),
			array('name'=>'level_id','width'=>15,'title'=>'等级','type'=>'radio','options'=>'level_types'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['level_types']=$this->level_types;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'01'=>'1月',
		'02'=>'2月',
		'03'=>'3月',
		'04'=>'4月',
		'05'=>'5月',
		'06'=>'6月',
		'07'=>'7月',
		'08'=>'8月',
		'09'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1);
	if($_RQ['level_id']){
		$params['level_id']=intval($_RQ['level_id']);
		$return['params']['level_id']=$_RQ['level_id'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(!empty($_RQ['y']) && !empty($_RQ['m'])){
		$yuestart = mktime(0,0,0,$_RQ['m'],1,$_RQ['y']);
		$yueend = mktime(0,0,0,$_RQ['m']+1,1,$_RQ['y']);
		$params['createtime between']=array($yuestart,$yueend);
	}elseif(!empty($_RQ['y'])){
		$yuestart = mktime(0,0,0,1,1,$_RQ['y']);
		$yueend = mktime(0,0,0,1,1,$_RQ['y']+1);
		$params['createtime between']=array($yuestart,$yueend);
	}
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$order=pdo_getall('jy_ylfee_member',$params,'','createtime desc');
	$month=$_RQ['y'].'-'.$_RQ['m'];
	foreach($order as &$item){
		$item['month']=$month;
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['nickname']=$item['member']['nickname'];
		$item['mobile']=$item['member']['mobile'];
		$item['address']=$item['province'].' '.$item['city'];
		$item['getfee0']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'status'=>1,'level'=>$types),'money');
		$item['getfee']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'level'=>$types),'money');
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	}
	$header=array(
		'month'=>'月份',
		'nickname'=>'代理商',
		'mid'=>'代理商ID',
		'mobile'=>'代理商电话',
		'address'=>'代理区域',
		'getfee0'=>'待提现绩效',
		'getfee'=>'总绩效',
		'createtime'=>'创建时间',
	);
	cfc('tools')->export2excel($header,$order,$month.'绩效表');
}
?>